<template>
  <view class="content-box">
    <!-- 搜索框 -->
    <view class="search-box">
      <view class="u-nav-slot">
        <image src="/static/image/chart/search.png" class="search-icon"></image>
        <input
          v-model="searchContent"
          placeholder="Search this chat"
          placeholder-class="placeholder-style"
        />
      </view>
      <view class="operation" @click.stop="handleCancel">{{
        $t("cancel")
      }}</view>
    </view>
    <view class="content">
      <view class="chat-list" v-if="list && list.length > 0">
        <view class="chat-item" v-for="(item, index) in list" :key="index">
          <image
            :src="item.info && item.info.userVo.avatar"
            mode="aspectFill"
            class="avator"
          ></image>
          <view class="right-wrap">
            <view class="info">
              <view class="name omit">{{
                formatUserName(item.info.userVo)
              }}</view>
              <view class="time">{{ weChatTimeFormat(item.sentTime) }}</view>
            </view>
            <view class="chat-content omit">{{ showLastMessages(item) }}</view>
          </view>
        </view>
      </view>
      <view v-else class="empty-result">
        <view class="iconfont icon-wujieguo"></view>
        <view class="empty-text"
          >{{ $t("search.resultEmptyLeft")
          }}<text class="empty-green">{{ $t("search.Me") }}</text
          >{{ $t("search.resultEmptyRight") }}</view
        >
      </view>
    </view>
  </view>
</template>

<script>
import { weChatTimeFormat } from "@/utils/common";
import { getHistoryMessages } from "@/utils/RongIM";
import { getUserInfo } from "@/utils/index";
import { mapState } from "vuex";
export default {
  data() {
    return {
      searchContent: "",
      targetId: "",
      chatList: [],
      conversationType: 1,
      targetUserInfo: {},
    };
  },
  computed: {
    ...mapState(["userId", "userInfo"]),
    list() {
      if (!this.searchContent) {
        return this.chatList;
      } else {
        return this.chatList.filter((item) => {
          return item.showLastMessages.match(this.searchContent);
        });
      }
    },
  },
  methods: {
    weChatTimeFormat,
    handleCancel() {
      this.searchContent = ""; /*  */
      uni.navigateBack();
    },
    async getChatList() {
      let list = await getHistoryMessages(
        this.targetId,
        this.conversationType
      );
      this.chatList = list
        .filter(
          (o) => o.messageType != "OL:ReadStatus" && o.messageType != "RC:RcCmd"
        )
        .map((item) => {
          item.showLastMessages = this.showLastMessages(item);
          if (this.conversationType == 1) {
            if (item.senderUserId == this.userId) {
              item.info = {
                userVo: this.userInfo
              }
            } else {
              item.info = {
                userVo: this.targetUserInfo
              }
            }
          }
          return item;
        });
      console.log(this.chatList);
    },
    showLastMessages(item) {
      let msg = "";
      switch (item.messageType) {
        case "RC:ImgMsg":
          msg = "[photo]";
          break;
        case "OL:Recall":
        case "RC:TxtMsg":
          msg = item.content.content;
          break;
        case "RC:SightMsg":
          msg = "[video]";
          break;
        case "RC:FileMsg":
          msg = item.content.name;
          break;
        default:
          break;
      }
      return msg;
    },
    // 获取用户信息
    async getTargetUserInfo() {
      this.targetUserInfo = await getUserInfo(this.targetId);
    },
  },
  async onLoad(options) {
    this.targetId = options.targetId;
    this.conversationType = Number(options.conversationType);
    if (this.conversationType == 1) {
      await this.getTargetUserInfo()
    }
    this.getChatList();
  },
};
</script>

<style lang="scss" scoped>
.omit {
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  line-clamp: 1;
  -webkit-box-orient: vertical;
  word-wrap: break-word;
  white-space: pre-wrap;
  word-break: break-word;
}
.empty-result {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 320rpx 0rpx 200rpx;
  .icon-wujieguo {
    font-size: 116rpx;
    color: #e6e6e6;
  }
  .empty-text {
    margin-top: 30rpx;
    font-size: 28rpx;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #878787;
    line-height: 33rpx;
    .empty-green {
      color: #61bdb2;
    }
  }
}
.content-box {
  width: 100%;
  box-sizing: border-box;
  height: 100vh;
  padding-top: calc(var(--status-bar-height) + 8rpx);
  .search-box {
    position: fixed;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 30rpx;
    margin-bottom: 30rpx;

    .u-nav-slot {
      padding: 0 24rpx;
      display: flex;
      align-items: center;
      width: 580rpx;
      height: 72rpx;
      line-height: 72rpx;
      background: #ffffff;
      border-radius: 56rpx;
      margin-right: 24rpx;
      .search-icon {
        width: 32rpx;
        height: 32rpx;
        margin-right: 8rpx;
      }
    }

    .operation {
      font-size: 28rpx;
      font-weight: 400;
      color: #878787;
      line-height: 40rpx;
      white-space: nowrap;
    }
  }
  .content {
    width: 100%;
    box-sizing: border-box;
    background: #ffffff;
    border-radius: 40rpx 40rpx 0 0;
    margin-top: calc(var(--status-bar-height) + 96rpx);
    height: calc(100vh - var(--status-bar-height) - 96rpx);
    padding: 0 30rpx;
    .chat-list {
      .chat-item {
        display: flex;
        align-items: center;
        height: 146rpx;
        background: #ffffff;
        line-height: 146rpx;
        box-shadow: inset 0px -2rpx 0px 0px rgba(0, 0, 0, 0.05);
        .avator {
          width: 70rpx;
          height: 70rpx;
          border-radius: 50%;
          background: #878787;
          margin-right: 20rpx;
        }
        .right-wrap {
          flex: 1;
          .info {
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 24rpx;
            font-weight: 400;
            color: #878787;
            line-height: 36rpx;
            margin-bottom: 10rpx;
            .name {
              flex: 1;
              margin-right: 10rpx;
            }
          }
          .chat-content {
            font-size: 28rpx;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.8);
            line-height: 40rpx;
          }
        }
      }
    }

    .empty-wrap {
      padding: 0 80rpx;
      text-align: center;
      .disabled {
        width: 300rpx;
        height: 200rpx;
        padding-top: 220rpx;
      }
      .status-text {
        font-size: 34rpx;
        font-family: Source Han Sans CN;
        font-weight: bold;
        color: #000000;
        opacity: 0.8;
        padding-top: 100rpx;
      }
      .status-tips {
        font-size: 24rpx;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #878787;
        padding-top: 40rpx;
      }
    }
  }
}
input {
  font-size: 28rpx;
  font-weight: 400;
  color: #000;
}
/deep/ .placeholder-style {
  font-size: 28rpx;
  font-weight: 400;
  color: #878787;
  line-height: 33rpx;
}
</style>